package com.tedu.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.tedu.pojo.SmbmsRole;
import com.tedu.pojo.SmbmsUser;
import com.tedu.services.UserPwdServices;
import com.tedu.services.UserRoleServices;
import com.tedu.services.UserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 处理与用户相关的交互
 */

@Controller
public class UserController { //用户模块的服务层接口，其实现类要交给spring管理

    @Autowired //该注解用户从spring容器中取出对象（通过名称、类型）
     UserServices userServices;

    @Autowired
    UserRoleServices userRoleServices;

    @Autowired
    UserPwdServices userPwdServices;


//登录
    @RequestMapping("/login")
    public String login(SmbmsUser smbmsUser, HttpSession session, Model model){


        System.out.println(smbmsUser);

        //调用服务层接口方法，实现登录操作
        SmbmsUser userByLogin = userServices.getUserByLogin(smbmsUser);
        //判断是否登录成功
        if (userByLogin!=null){
            //登录成功
            session.setAttribute("userOnLogin",userByLogin);
            return "frame"; //项目主页
        }else{
            //登录失败
            model.addAttribute("error","您输入的用户名密码错误，请重新登录~");
            return "login";
        }

    }
//退出
    @RequestMapping("/sys/logout")
    public String logout(HttpSession session,Model model){
        session.removeAttribute("userOnLogin");
        model.addAttribute("error","您已经成功退出~");
        return "login";
    }

    //用户管理 全部用户信息
    @RequestMapping("/sys/user")
    public  String getAllUser(Model model){
        //处理器方法
        List<SmbmsUser> allUser = userServices.getAllUser();
        //发送数据到视图
        model.addAttribute("userList",allUser);
        return "/user/userlist";
    }

    //添加用户
    @RequestMapping("/sys/useradd")
    public String addUser(Model model){


        List<SmbmsRole> allRole = userRoleServices.getAllRole();
        model.addAttribute("allRole",allRole);
        return "/user/useradd";

    }

    //用户信息查看
    @RequestMapping("/sys/viewUser")
    public  String userView(int uid,Model model){
        //调用服务层方法，获取用户信息
       SmbmsUser user =  userServices.getUserById(uid);
        //把用户信息发送给用户
        model.addAttribute("user",user);

        //角色信息

       SmbmsRole role =  userRoleServices.getRoleById(user.getUserrole());
        model.addAttribute("roleName",role.getRolename());
        return "/user/userview";


    }

    /**
     * 显示用户信息修改页面---还未进行修改
     * @param uid
     * @param model
     * @return
     */

    @RequestMapping("/sys/modifyUser")
    public String toUpdateUser(int uid,Model model){
        //调用服务层方法，获取用户信息
        SmbmsUser user =  userServices.getUserById(uid);
        //把用户信息发送给用户
        model.addAttribute("user",user);

        //角色信息
        SmbmsRole role =  userRoleServices.getRoleById(user.getUserrole());
        model.addAttribute("roleName",role.getRolename());
        return "/user/usermodify";
    }

    /**
     * 获取全部的用户角色信息，返回的json数据格式
     * @return
     */
    @RequestMapping("/sys/getrolelist")
    public String getAllRole() throws JsonProcessingException {

        //服务方法，获取全部用户角色信息
        List<SmbmsRole> allRole = userRoleServices.getAllRole();
        //转换json数据格式
        //创建对象
        ObjectMapper mapper = new ObjectMapper();
        //序列化对象
        String string = mapper.writeValueAsString(allRole);
        System.out.println("json:" + string);
        return string;

    }





    //修改密码实现方法
    @RequestMapping("/sys/modifyPasswordGo")
    public String modifyPasswordGo(String userCode,String userPassword,String newPassword,Model md) {
        String msg;
        if(userServices.modifyPasswd(userCode, userPassword, newPassword))
            msg="修改密码成功！";
        else
            msg="修改密码失败！";
        md.addAttribute("msg",msg);
        return "modifyPasswordFinish";
    }

    //添加用户打开修改密码的界面
    @RequestMapping("/sys/pwdmodify")
    public String modifyPasswd() {
        return "/user/pwdmodify";
    }


}
